<?php
/**
 * 文件上传管理接口
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/11/9
 * Time: 16:59
 */

namespace app\api\controller\v1;


use app\api\service\FileUpload;
use app\api\service\Token;
use app\lib\exception\FileUplodeException;
use app\lib\exception\SuccessMessage;

class File
{
    /**
     * 请求资源上传token
     * @return string
     */
    public function getUploadToken($type){
        $uid = Token::getCurrentUid();
        $service = new FileUpload();
        $token = $service->getUploadToken($uid,$type);
        $info['token'] = $token;
        $info['url'] = 'resource.17jxyx.com';//上传地址
        return json($info);
    }

    /**
     * 7牛云上传数据回调
     * @return \think\response\Json
     * @throws FileUplodeException
     * @throws \think\exception\DbException
     */
    public function getCallbackInfo(){
        $service = new FileUpload();
        $result =  $service->getCallbackInfo();
           if ($result){
              $array=[
                  'key'=>$result->key,
                  'img_id'=>$result->id,
                  'name'=>$result->name
              ];
               return json($array);//上传成功
           }else{
               return json('',500);//上传失败
           }
    }

    /**
     * 上传数据处理回调函数
     * @return \think\response\Json
     * @throws FileUplodeException
     */
    public function getPersistentCallbackInfo(){
        $service = new FileUpload();
        $result =  $service->getPersistentCallbackInfo();
        if ($result){
            $array=['ret'=>'success'];
            return json($array);//上传成功
        }else{
            return json('',500);//上传失败
        }
    }



    /**
     * 删除图片
     * @param $img_id
     * @param $type
     * @return \think\response\Json
     * @throws FileUplodeException
     * @throws \think\exception\DbException
     */
    public function deleteImg($img_id,$type){
        $uid= Token::getCurrentUid();
        $service = new FileUpload();
        $result  = $service->deleteFile($uid,$img_id,$type);
        if ($result != null){
            throw new FileUplodeException([
                'code'=>404,
                'msg'=>"删除图片文件不存",
                'error_code'=>90000
            ]);
        }
        return json(new SuccessMessage(),201);
    }



}